package com.zwr.sheller.shop.service.impl;

import com.zwr.sheller.core.common.WQQBusinessException;
import com.zwr.sheller.core.common.WQQExceptionCode;
import com.zwr.sheller.core.pojo.Sign;
import com.zwr.sheller.core.util.GetThreadLocal;
import com.zwr.sheller.core.util.JsonUtil;
import com.zwr.sheller.core.util.OSSUtil;
import com.zwr.sheller.core.util.ThreadLocalUtil;
import com.zwr.sheller.shop.mapper.SignMapper;
import com.zwr.sheller.shop.service.SignService;
import org.junit.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 18032
 * @Date: 10:43 2022/1/10
 */
@Service
public class SignServiceImpl implements SignService {
    @Autowired(required = false)
    SignMapper signMapper;

    @Override
    public Integer insertSign(Sign sign) throws WQQBusinessException {
        Map<String, Object> map = new HashMap<>(1);
        map.put("storeId", 1L);
        ThreadLocalUtil.setObject(map);
        Long storeId = GetThreadLocal.getStoreId();
        sign.setStoreId(storeId);
        //复制图片到正式文件
        if (!StringUtils.isEmpty(sign.getSignImg())) {
            String fileName =sign.getSignImg().substring(37);
            OSSUtil.copyFile("temporary/" + fileName, "signPicture" + fileName);
            OSSUtil.delete("temporary/" + fileName);
            sign.setSignImg("http://47.108.60.93/ossImg/storePicture" + fileName);
        }

        int i = signMapper.insertSelective(sign);
        if (i == 0) {
            throw new WQQBusinessException(WQQExceptionCode.UPDATE_FALSE);
        }
        return i;
    }

    @Override
    public List<Sign> selectAllSigns() {
        Map<String, Object> map = new HashMap<>(1);
        map.put("storeId", 1L);
        map.put("storeIID", "213");
        ThreadLocalUtil.setObject(map);

        Long storeId = GetThreadLocal.getStoreId();
        Example example = new Example(Sign.class);
        example.createCriteria().andEqualTo("storeId", storeId);
        List<Sign> signs = signMapper.selectByExample(example);
        return signs;
    }

    @Override
    public Integer updateSign(Sign sign) throws WQQBusinessException {
        Map<String, Object> map = new HashMap<>(1);
        map.put("storeId", 1L);
        ThreadLocalUtil.setObject(map);

        Long storeId = GetThreadLocal.getStoreId();
        sign.setStoreId(storeId);
        System.out.println("这是一个sign:" + sign);
        //复制图片到正式文件
        if (!StringUtils.isEmpty(sign.getSignImg())) {
            String fileName =sign.getSignImg().substring(37);
            OSSUtil.copyFile("temporary/" + fileName, "signPicture" + fileName);
            OSSUtil.delete("temporary/" + fileName);
            sign.setSignImg("http://47.108.60.93/ossImg/storePicture" + fileName);
        }
        int i = signMapper.updateByPrimaryKeySelective(sign);
        if (i == 0) {
            throw new WQQBusinessException(WQQExceptionCode.UPDATE_FALSE);
        }
        return i;
    }

    @Override
    public Integer deleteSign(Long signId) throws WQQBusinessException {
        int i = signMapper.deleteByPrimaryKey(signId);
        if (i == 0) {
            throw new WQQBusinessException(WQQExceptionCode.DELETE_FAILED);
        }
        return i;
    }
}
